<?php


namespace app\admin\controller\goods;


use app\common\model\GoodsCate;
use app\common\model\Goods as GoodsModel;
use app\admin\service\goods\GoodsService;
use app\admin\service\goods\AttrService;
use app\common\controller\BaseAdmin;
use app\admin\model\GoodsAttrType;
use think\response\Json;
use think\facade\Config;

class Goods extends BaseAdmin
{

    /**
     * 商品列表
     **/
    public function index()
    {
        if ($this->request->isAjax()) {
            //获取搜索条件
            $data = $this->request->except(['page'], 'get');

            $model = new GoodsModel();
            $list = $model->withSearch($this->searchFields($data), $this->searchData($data))
                ->order('id', 'desc')
                ->paginate($data['limit'], false);
            foreach ($list as $key=>$vs){
                $list[$key]['start_time']=date('Y-m-d H:i:s',$vs['start_time']);
                $list[$key]['end_time']=date('Y-m-d H:i:s',$vs['end_time']);
            }

            return $this->tableJson($list->getCollection(), $list->total());
        }

        //分类列表选择
        $cate = GoodsCate::where('state', 1)->order('id')->column('id,name,parent_id');
        $cate = $this->getTreeOrder($cate); //分类递归处理
        $this->assign('cate', array_column($cate, 'levelName', 'id'));

        return $this->fetch('goods/index');
    }

    /**
     * 新建/编辑模板
     * @return array|string
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            //编辑或者添加数据
            $scene = empty($data['id']) ? 'add' : 'edit';

            return json(GoodsService::storeGoodsCate($data, $scene));
        }

        //商品分类
        $cate = GoodsCate::where('state', 1)->order('id')->column('id,name,parent_id');
        $cate = $this->getTreeOrder($cate); //分类递归处理
        $this->assign('cate', $cate);

        //属性分类
        $attr_cate = GoodsAttrType::where(['state' => 1])->field('id,name')->select()->toArray();
        $this->assign('attr_cate', $attr_cate);

        $id = $this->request->get('id');
        if ($id) {
            $list = GoodsModel::where('id', $id)->find();
            $images = [];
            if (!empty($list->images)) {
                $images = explode(',', $list->images);
            }
//            $list->start_time=date('Y-m-d H:i:s',$list->start_time);
//            $list->end_time=date('Y-m-d H:i:s',$list->end_time);
            // print_r($images);exit;
            $this->assign('images', $images);
            $this->assign('list', $list);
        }

        return $this->fetch('goods/edit');
    }

    /**
     * 商品属性
     */
    public function attr_html()
    {
        $id = $this->request->get('id');
        if (empty($id)) {
            $this->error('非法请求');
        }
        //属性分类
        $attr_cate = GoodsAttrType::where(['state' => 1])->field('id,name')->select()->toArray();
        $this->assign('attr_cate', $attr_cate);
        //获取指定id商品
        $list = GoodsModel::where('id', $id)->field('id,name,thumb,attr_id')->find();
        //获取商品已选的属性值
        $goods_attr = GoodsService::getGoodsAttrname($list->id);
//        print_r($goods_attr);
//        exit;
        $this->assign('list', $list);
        $this->assign('spec', $goods_attr['attr_arr']);
        $this->assign('type_name', $goods_attr['type_name']);
        $this->assign('attrid', $goods_attr['attr_id']);
        return $this->fetch('goods/attr_html');
    }

    /**
     * 商品属性(保存)
     */
    public function goods_attr()
    {
        $data = $this->request->param();

        GoodsService::saveGoodsAttr($data);
        return json(['code' => 200, 'result' => '']);
    }


    /**
     * 删除批量删除
     */
    public function remove()
    {
        if ($this->request->isAjax()) {
            $ids = $this->request->param('ids');

            GoodsModel::destroy($ids);

            //连带删除用户角色关系
            if (!is_array($ids)) {
                $uid[] = $ids;
            } else {
                $uid = $ids;
            }

            AuthGroupAccess::where('uid', 'in', $uid)->delete();
            $this->success('删除成功');

        } else {
            $this->error('非法请求');
        }
    }

    /**
     * 获取商品规格组合
     */
    public function get_attr()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();
            if (!empty($params) && isset($params['type'])) {
                $data = AttrService::getAttrArry($params);
//                print_r($data);exit;
                return json(['code' => 200, 'result' => $data]);
            }
        } else {
            $this->error('非法请求');
        }
    }

    /**
     * 状态
     */
    public function change()
    {
        $id = $this->request->param('id');
        $model = GoodsModel::find($id);
        $model->state = $this->request->param('state');
        if ($model->save()) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 推荐 状态
     */
    public function recommend()
    {
        $id = $this->request->param('id');
        $model = GoodsModel::find($id);
        $model->is_recommend = $this->request->param('state');
        if ($model->save()) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

}